﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfMN200_YH.Connect
{
    class CSC610LightControl
    {
        static SerialPortDemo3.ConnectSerialPortHelper connect => ConnectFactory.SerialPort_light;
        public static void Control(byte channel, LightCMD lightCMD, int data)
        {
            if(channel < 0||channel >= 4)
            {
                throw new Exception($"通道设置错误，无法设置{channel}通道，通道范围：0~3");
            }
            //声明一个bool数组
            byte[] tempbytes = new byte[6];

            int setData = 0;

            if (lightCMD == LightCMD.CMD_SET_ONOFF || lightCMD == LightCMD.CMD_SET_ACTIVE || lightCMD == LightCMD.CMD_SET_ONOFF_SELECT)
            {
                setData = ((data & 0x00FF) << 8);
            }
            else
            {
                setData = data;
            }

            tempbytes[0] = 0x4C;
            tempbytes[1] = (byte)lightCMD;
            tempbytes[2] = channel;

            tempbytes[3] = (byte)((setData >> 8) & 0x00FF);
            tempbytes[4] = (byte)(setData & 0x00FF);
            tempbytes[5] = 0;

            for (int i = 0; i < 5; i++)
            {
                tempbytes[5] += tempbytes[i];
            }

            connect.SendBytes(tempbytes);
        }
        public static void Light_ON(byte channel)
        {
            Control(channel, LightCMD.CMD_SET_ONOFF_SELECT, 1);
        }

        public static void Light_OFF(byte channel)
        {
            Control(channel, LightCMD.CMD_SET_ONOFF_SELECT, 0);
        }
    }

    public enum LightCMD
    {
        /// <summary>
        /// 读取控制器状态及其应答
        /// </summary>
        CMD_GET_STATUS = 0x41,

        /// <summary>
        /// 设置当前的通道ON、OFF
        /// </summary>
        CMD_SET_ONOFF = 0x42,

        /// <summary>
        /// 设置色温
        /// </summary>
        CMD_SET_TCP = 0x43,

        /// <summary>
        /// 设置照度
        /// </summary>
        CMD_SET_EV = 0x44,

        /// <summary>
        /// 设置Active(通道激活或去掉)
        /// </summary>
        CMD_SET_ACTIVE = 0x45,

        /// <summary>
        /// 自动校准
        /// </summary>
        CMD_CALIBRATION = 0x46,

        /// <summary>
        /// 通道选择
        /// </summary>
        CMD_SELECT_CHANNEL = 0x47,

        /// <summary>
        /// 指定的通道ON/OFF
        /// </summary>
        CMD_SET_ONOFF_SELECT = 0x55


    }
}
